package com.ruoyi.project.wenyanwen.generate.service.impl;

import com.ruoyi.common.constant.DbGenerateCommand;
import com.ruoyi.common.constant.DbGenerateStatus;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.wenyanwen.generate.domain.DbGenerate;
import com.ruoyi.project.wenyanwen.generate.mapper.DbGenerateMapper;
import com.ruoyi.project.wenyanwen.generate.service.IDbGenerateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 数据包生成Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-09-27
 */
@Service
@DataSource(DataSourceType.KKWENYANWEN)
public class DbGenerateServiceImpl implements IDbGenerateService {

    private BlockingQueue<DbGenerate> queue = new ArrayBlockingQueue<DbGenerate>(32);


    @Autowired
    private DbGenerateMapper dbGenerateMapper;

    /**
     * 查询数据包生成
     *
     * @param id 数据包生成ID
     * @return 数据包生成
     */
    @Override
    public DbGenerate selectDbGenerateById(Long id) {
        return dbGenerateMapper.selectDbGenerateById(id);
    }

    /**
     * 查询数据包生成列表
     *
     * @param dbGenerate 数据包生成
     * @return 数据包生成
     */
    @Override
    public List<DbGenerate> selectDbGenerateList(DbGenerate dbGenerate) {
        return dbGenerateMapper.selectDbGenerateList(dbGenerate);
    }

    /**
     * 新增数据包生成
     *
     * @param dbGenerate 数据包生成
     * @return 结果
     */
    @Override
    public int insertDbGenerate(DbGenerate dbGenerate) {
        return dbGenerateMapper.insertDbGenerate(dbGenerate);
    }

    /**
     * 修改数据包生成
     *
     * @param dbGenerate 数据包生成
     * @return 结果
     */
    @Override
    public int updateDbGenerate(DbGenerate dbGenerate) {
        return dbGenerateMapper.updateDbGenerate(dbGenerate);
    }

    /**
     * 删除数据包生成对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDbGenerateByIds(String ids) {
        return dbGenerateMapper.deleteDbGenerateByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除数据包生成信息
     *
     * @param id 数据包生成ID
     * @return 结果
     */
    @Override
    public int deleteDbGenerateById(Long id) {
        return dbGenerateMapper.deleteDbGenerateById(id);
    }

    @Override
    public DbGenerate getTask() {
        try {
            return queue.take();
        } catch (InterruptedException ie) {
            return null;
        }
    }

    @Override
    public boolean addTask(String dbName, String command, String username) {

        DbGenerate query = new DbGenerate();
        query.setDbName(dbName);
        List<DbGenerate> dbGenerates = this.dbGenerateMapper.selectDbGenerateList(query);
        DbGenerate dbGenerate = dbGenerates.get(0);
        if (dbGenerate.getStatus() == DbGenerateStatus.DOING && StringUtils.equalsIgnoreCase(DbGenerateCommand.GENERATE, command)) {
            throw new RuntimeException("有正在运行的任务，请等待完成或者取消后再开始新的任务");
        }

        if (StringUtils.equalsIgnoreCase(DbGenerateCommand.GENERATE, command)) {
            dbGenerate.setStatus(DbGenerateStatus.DOING);
        } else {
            dbGenerate.setStatus(DbGenerateStatus.CANCEL);
        }
        dbGenerate.setLastResult(0);
        dbGenerate.setCommand(command);
        dbGenerate.setProgress(0L);
        dbGenerate.setCommandUser(username);

        queue.add(dbGenerate);

        return this.dbGenerateMapper.updateDbGenerate(dbGenerate) > 0;
    }

}
